package com.bysun.moduals.finance.controller;

import com.bysun.moduals.finance.pojo.entity.TbBill;
import com.bysun.moduals.finance.pojo.enums.ResultEnum;
import com.bysun.moduals.finance.pojo.vo.ResultVo;
import com.bysun.moduals.finance.service.FinBillService;
import com.bysun.moduals.finance.service.common.IQiNiuService;
import com.bysun.moduals.finance.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Calendar;

/**
 * @author bysun
 */
@Controller
public class FinBillController {

    @Autowired
    private FinBillService finBillService;
    @Autowired
    private IQiNiuService iQiNiuService;
    @Value("${qiniu.cdn.prefix}")
    private String pre;

    private String getCurrentYear() {
        Calendar instance = Calendar.getInstance();
        int i = instance.get(Calendar.YEAR);
        return String.valueOf(i);
    }

    private String getCurrentMonth() {
        Calendar instance = Calendar.getInstance();
        int i = instance.get(Calendar.MONTH);
        if (1 < 10) {
            return "0" + String.valueOf(i);
        }
        return String.valueOf(i);
    }

    @GetMapping(value = "goToBills")
    public String goToBills(String userId, String year, Model model) {
        if (StringUtils.isBlank(year)) {
            year = getCurrentYear();
        }
        model.addAttribute("bills", finBillService.getBillsByUserIdAndYear(userId, year));
        return "bills";
    }

    @GetMapping(value = "goToRunningAccount")
    public String goToRunningAccount(String userId, String year, String month, Integer pageNo, Integer pageSize, Model model) {
        if (StringUtils.isBlank(year)) {
            year = getCurrentYear();
        }
        if (StringUtils.isBlank(month)) {
            month = getCurrentMonth();
        }
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        model.addAttribute("runningAccount", finBillService.getRunningAccountByUserIdAndYearAndMonth(userId, year, month, pageNo, pageSize));
        return "runningAccount";
    }

    @GetMapping(value = "account")
    public String account(String id, Model model) {
        model.addAttribute("account", finBillService.getAccountById(id));
        return "account";
    }

    @DeleteMapping(value = "delAccount")
    public String delAccount(String id, String userId, String year, String month, Integer pageNo, Integer pageSize, Model model) {
        finBillService.delAccountById(id);
        return "redirect:goToRunningAccount?userId=" + userId +"&year=" + year +"&month=" + month;
    }

    @ResponseBody
    @PostMapping("uploadFile")
    public ResultVo updateFile(MultipartFile file) {
        String path;
        try {
            path = iQiNiuService.uploadFile(file.getInputStream());
        } catch (IOException e) {
            return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
        }
        return ResultUtil.success(pre + "/" + path);
    }

    @PostMapping(value = "addAccount")
    public String addAccount(TbBill bill) {
        finBillService.addAccount(bill);
        return "redirect:goToBills?userId=1";
    }

    @PutMapping(value = "addAccount")
    public String updateAccount(TbBill bill) {
        finBillService.updateAccount(bill);
        return "redirect:goToBills?userId=1";
    }

    @GetMapping(value = "gotoAddPage")
    public String gotoAddPage(){
        return "account";
    }
}
